#include <iostream>
#include <allegro.h>

#include "util.h"
#include "joypad.h"
#include "title.hpp"

int title() {
	bool selected=false;
	int sel=0;
	const int maxsel=3;
	int delay=60;

	BITMAP *TBG=load_bitmap("bitmaps/title.tga", NULL);

	if (!TBG) fatalError("Cannot found \"title.tga\"!");
	
	clear_keybuf();
	
	while (!selected) {
		if (key[KEY_RIGHT] && delay<=0) {
			sel++;
			delay=60;
		} else if (key[KEY_LEFT] && delay<=0) {
			sel--;
			delay=60;
		} else if (key[KEY_SPACE] or key[KEY_ENTER] && delay<=0) {
			selected=true;
		} else if (key[KEY_ESC] && delay<=0) {
			selected=true;
			return 0;
		}
		
		if (delay>0) delay--;
		
		if (sel<=0) sel=maxsel;
		if (sel>maxsel) sel=1;
		
		
		blit(TBG, doubleBuffer, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
		if (sel==1)  textout_centre_ex(doubleBuffer, font, "INFLATEC", 320, 320, makecol(0, 0, 0), -1);
		else if (sel==2)  textout_centre_ex(doubleBuffer, font, "HYDROTEC", 320, 320, makecol(0, 0, 0), -1);
		else if (sel==3)  textout_centre_ex(doubleBuffer, font, "LISTECTRIX", 320, 320, makecol(0, 0, 0), -1);
		
		show();
	}
	clear_keybuf();
	
	destroy_bitmap(TBG);
    
	return sel;
}
